package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.master.EntityWechatAppInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoWechatAppInfo;
import com.ocom.payconfig.service.WechatAppInfoService;
import org.springframework.stereotype.Service;

@Service
public class WechatAppInfoServiceImpl extends ServiceImpl<IDaoWechatAppInfo, EntityWechatAppInfo> implements WechatAppInfoService {


	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
	public Boolean addInfo(EntityWechatAppInfo entityWechatAppInfo) {
		UpdateWrapper<EntityWechatAppInfo> updateWrapper=new UpdateWrapper();
		updateWrapper.eq("com_id" , entityWechatAppInfo.getComId());
		updateWrapper.eq("merch_id" , entityWechatAppInfo.getMerchId());
		updateWrapper.set("appid",entityWechatAppInfo.getAppid());
		updateWrapper.set("appsecret",entityWechatAppInfo.getAppsecret());
		updateWrapper.set("app_name",entityWechatAppInfo.getAppName());
		return this.saveOrUpdate(entityWechatAppInfo,updateWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
	public EntityWechatAppInfo getInfo(Long comId, Long merchId,String appName) {
		QueryWrapper<EntityWechatAppInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("com_id" , comId);
		if(merchId == 0 || merchId == -1){
			queryWrapper.and(wrapper -> {
				wrapper.or().eq("merch_id",0).or().eq("merch_id",-1);
			});
		}else{
			queryWrapper.eq("merch_id" , merchId);
		}
		queryWrapper.apply("app_name like '"+appName+"%'" );
		queryWrapper.orderByDesc("create_time");
		queryWrapper.last("limit 1");
		return this.getOne(queryWrapper);
	}


}
